package ods_field

import (
	"fmt"
	"server/global"
	"server/model/common/request"
	"server/model/common/response"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type OdsDbFieldApi struct {
}

var odsDbFieldService = OdsDbFieldService{}

func (odsDbFieldApi *OdsDbFieldApi) CreateOdsDbField(c *gin.Context) {
	var odsDbField OdsDbField
	err := c.ShouldBindJSON(&odsDbField)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := odsDbFieldService.CreateOdsDbField(&odsDbField); err != nil {
		global.Logger.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

func (odsDbFieldApi *OdsDbFieldApi) DeleteOdsDbField(c *gin.Context) {
	var odsDbField OdsDbField
	err := c.ShouldBindJSON(&odsDbField)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := odsDbFieldService.DeleteOdsDbField(odsDbField); err != nil {
		global.Logger.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

func (odsDbFieldApi *OdsDbFieldApi) DeleteOdsDbFieldByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := odsDbFieldService.DeleteOdsDbFieldByIds(IDS); err != nil {
		global.Logger.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

func (odsDbFieldApi *OdsDbFieldApi) UpdateOdsDbField(c *gin.Context) {
	var odsDbField OdsDbField
	err := c.ShouldBindJSON(&odsDbField)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := odsDbFieldService.UpdateOdsDbField(odsDbField); err != nil {
		global.Logger.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

func (odsDbFieldApi *OdsDbFieldApi) FindOdsDbField(c *gin.Context) {
	var odsDbField OdsDbField
	err := c.ShouldBindQuery(&odsDbField)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if reodsDbField, err := odsDbFieldService.GetOdsDbFieldById(odsDbField.ID); err != nil {
		global.Logger.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reodsDbField": reodsDbField}, c)
	}
}

func (odsDbFieldApi *OdsDbFieldApi) GetOdsDbFieldList(c *gin.Context) {
	var pageInfo OdsDbFieldSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := odsDbFieldService.GetOdsDbFieldInfoList(pageInfo); err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

func (odsDbFieldApi *OdsDbFieldApi) GetAllOdsDbFields(c *gin.Context) {
	odsDbField, err := odsDbFieldService.GetAllOdsDbFields()
	if err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(gin.H{"odsDbField": odsDbField}, "获取成功", c)
	}
}


func (odsDbFieldApi *OdsDbFieldApi) GetOdsTbFld(c *gin.Context) {
	response.FailWithMessage("此方法已经弃用！！", c)
}

func (odsDbFieldApi *OdsDbFieldApi) SaveOdsTbFld(c *gin.Context) {
	response.FailWithMessage("此方法已经弃用！！", c)
}


func (odsDbFieldApi *OdsDbFieldApi) GetOdsFldsByTbId(c *gin.Context) {
	var tbId int
	err := c.ShouldBindJSON(&tbId)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	tbIdString := fmt.Sprintf("%d", tbId)
	odsFlds, err1 := odsDbFieldService.GetOdsDbFieldByCol("odsTbId", tbIdString, "colName")
	if err != nil {
		global.Logger.Error("查询失败!", zap.Error(err1))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"odsFlds": odsFlds}, c)
	}
}
